package de.karlNet.gifSocket.client;

import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;

import org.apache.http.client.ClientProtocolException;

import de.karlNet.gifSocket.controllers.SessionController;
import de.karlNet.gifSocket.controllers.interfaces.ISessionController;

public class ScreenShotterNG {

	/**
	 * @param args
	 * @throws AWTException
	 * @throws IOException 
	 * @throws ClientProtocolException 
	 */
	public static void main(String[] args) throws AWTException, ClientProtocolException, IOException {
		final ISessionController sessionController = new SessionController(
				"http://localhost:8080/");
		final Rectangle captureSize = new Rectangle(Toolkit.getDefaultToolkit()
				.getScreenSize());

		final Integer sessionNumber = sessionController
				.createSession(captureSize);
		System.out.println(sessionNumber);
		final Robot robot = new Robot();
		Thread screenshotterThread = new Thread(new Runnable() {

			public void run() {
				try {
					while (!Thread.currentThread().isInterrupted()) {

						final BufferedImage imageToAdd = (BufferedImage) robot
								.createScreenCapture(captureSize);
						sessionController.addPictureToSession(sessionNumber,
								imageToAdd);
					}
				} catch (ClientProtocolException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		screenshotterThread.start();
		Thread clickReaderThread = new Thread(new Runnable() {
			
			public void run() {
				try {
					sessionController.addMouseListener(sessionNumber, null);
				} catch (ClientProtocolException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		});
		clickReaderThread.start();
	}

}
